{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HELLO, ENTANGLED WORLD!\n",
    "\n",
    "As an example of how we can work with two qubits in Qiskit, we are going to implement a circuit that prepares an entangled state (a Bell pair)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Acer\\.conda\\envs\\name_of_my_env\\lib\\site-packages\\numpy\\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:\n",
      "C:\\Users\\Acer\\.conda\\envs\\name_of_my_env\\lib\\site-packages\\numpy\\.libs\\libopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll\n",
      "C:\\Users\\Acer\\.conda\\envs\\name_of_my_env\\lib\\site-packages\\numpy\\.libs\\libopenblas.PYQHXLVVQ7VESDPUVUADXEVJOBGHJPAY.gfortran-win_amd64.dll\n",
      "  warnings.warn(\"loaded more than 1 DLL from .libs:\\n%s\" %\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAUoAAACoCAYAAACG0qc4AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAUMklEQVR4nO3dfVDVdaLH8fc5PKoQiPgIPqGCwgYiYGZ5fSBbLZu8+TSWuraYXrQdtZrM2Vt7d9oLau72dGdXzcpt6zp3RMpqbCs3Jc26V9Z8wCgkNB4CfABtUcHknPsHyXoU+GJxzs8Dn9fMmTl8fw/n4wMfvr+Hc7A5nU4nIiLSLLvVAUREbnQqShERAxWliIiBilJExEBFKSJioKIUETFQUYqIGKgoRUQMVJQiIgYqShERAxWliIiBilJExEBFKSJioKIUETFQUYqIGKgoRUQMVJQiIgYqShERAxWliIiBilJExEBFKSJioKIUETFQUYqIGKgo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9GiRURGRlJfX09kZCRLlixp65htyubU/3L5ifIKjrH373nMvieV4KDOVsfxKg6nkz/9ZRsl5SdcxoM6d+KxhbMIDPC3KFnLDuV/zX+//bdrxu+deBu3joizIJF7aUZ5HbZt28aUKVPo0aMHAQEB9O/fn/vvv5/Dhw9bHc0yDqeTv32yn+9qztO5c6DVcbyO3WbjntRbrxm/c0zyDVuSADcPjWJApOvtPT26dWXk8GEWJXIvFWUrXLp0iQceeICpU6dy+PBh7rvvPpYuXUpiYiJbt26lrKzM6oiW+eLoccpPnCZ19Ah87Prv9GP0i+jJ8NjBjV/36h5GcnyMhYnMbDYbU1JvxXbF2JTUW9vt/wEderdCeno669at4+GHH+bZZ591edtVaWkpISEhBAcHt8lrPbF6Q5vsR0TMVq1Y2Kr1dMO5we7du1m3bh2TJk3ihRdewGazuSx3560YInJj0IzSYNq0aWRnZ3PgwAESEhKsjnPDcDidvLgpm+8vXWJ52ox2e8jlSRe/v0TN+QuEhbTN0YmnVJ6qpmd4V6tjuJWK0iA4OJjw8HCOHTvmkdfTobeI57T20FvTgBacOXOGmpoaBgwYYHUUEbGQZpQtqK6uJiwsjNjYWI4cOWJ1nBtGXsExXn/zQ2ZNGU9i3BCr44i4nWaULejatStRUVHk5+ezY8eOa5Z/9dVXFqSy1uX7JsPDQogfNsjqOCIeoaveBpmZmcyaNYvJkydz7733MmjQIE6cOMHevXuJjY3lzTfftDqiR507fwEfu51/0X2T0oHo0LsV3n//fZ555hn27dtHbW0tPXr0YOTIkSxbtowxY8ZYHc/jnE4nThreVSLSEagoRUQMdOwkImKgohQRMVBRiogYqChFRAxUlCIiBipKEREDFaWIiIGKUkTEQEUpImKgohQRMVBRiogYqChFRAxUlCIiBipKEREDFaWIiIGKUkTEQEUpImKgohQRMVBRiogYqChFRAxUlCIiBipKEREDFaWIiIGKUkTEQEUpImKgohQRMVBRiogY/D819drtUv+1swAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 418.992x204.68 with 1 Axes>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "from qiskit import *\n",
    "from qiskit.visualization import *\n",
    "from qiskit.tools.monitor import *\n",
    "\n",
    "circ_bell = QuantumCircuit(2,2) # We need two qubits and two classical bits (for the measurements)\n",
    "\n",
    "circ_bell.h(0) # We apply the H gate on the first qubit\n",
    "circ_bell.cx(0,1) # We apply the CNOT gate with control on the first qubit and target on the second\n",
    "circ_bell.measure(range(2),range(2)) # Measurement\n",
    "\n",
    "circ_bell.draw(output='mpl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We execute the circuit 1000 times and print the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'00': 496, '11': 504}\n"
     ]
    }
   ],
   "source": [
    "backend = Aer.get_backend('qasm_simulator')\n",
    "job = execute(circ_bell, backend, shots=1000)\n",
    "\n",
    "counts = job.result().get_counts()\n",
    "\n",
    "print(counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we are going to directly access the amplitudes and probabilities of the states. For that, we need to remove the measurement and use the statevector simulator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Amplitude of 00 = (0.7071067811865476+0j)\n",
      "Amplitude of 01 = 0j\n",
      "Amplitude of 10 = 0j\n",
      "Amplitude of 11 = (0.7071067811865476+0j)\n",
      "\n",
      "Probability of 00 = 0.5000000000000001\n",
      "Probability of 01 = 0.0\n",
      "Probability of 10 = 0.0\n",
      "Probability of 11 = 0.5000000000000001\n"
     ]
    }
   ],
   "source": [
    "circ_bell2 = QuantumCircuit(2)\n",
    "circ_bell2.h(0) \n",
    "circ_bell2.cx(0,1) \n",
    "\n",
    "backend = Aer.get_backend('statevector_simulator')\n",
    "job = execute(circ_bell2, backend)\n",
    "\n",
    "state = job.result().get_statevector()\n",
    "\n",
    "for i in range(4):\n",
    "    s = format(i,\"b\") # Convert to binary\n",
    "    s = (2-len(s))*\"0\"+s # Prepend zeroes if needed\n",
    "    print(\"Amplitude of\",s,\"=\",state[i])\n",
    "    \n",
    "print()\n",
    "\n",
    "for i in range(4):\n",
    "    s = format(i,\"b\") # Convert to binary\n",
    "    s = (2-len(s))*\"0\"+s # Prepend zeroes if needed\n",
    "    print(\"Probability of\",s,\"=\",abs(state[i])**2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are going to run the circuit on an actual computer. To reduce the wait, we would like to execute on the computer with the fewest pending jobs. We can check their properties, after loading our account, with the order *backend_overview()*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ibmqfactory.load_account:WARNING:2020-11-14 02:33:08,261: Credentials are already in use. The existing account in the session will be replaced.\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,919: Remote backend \"ibmq_qasm_simulator\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,921: Remote backend \"ibmqx2\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,923: Remote backend \"ibmq_16_melbourne\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,925: Remote backend \"ibmq_vigo\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,927: Remote backend \"ibmq_ourense\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,930: Remote backend \"ibmq_valencia\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,933: Remote backend \"ibmq_armonk\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,935: Remote backend \"ibmq_athens\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n",
      "accountprovider._discover_remote_backends:WARNING:2020-11-14 02:33:12,937: Remote backend \"ibmq_santiago\" could not be instantiated due to an invalid config: ModelValidationError: \"{'online_date': ['Not a valid datetime.']}\"\n"
     ]
    },
    {
     "ename": "QiskitError",
     "evalue": "'No backends available.'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mQiskitError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-b4390f79c67d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mprovider\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mIBMQ\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload_account\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mbackend_overview\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\.conda\\envs\\name_of_my_env\\lib\\site-packages\\qiskit\\tools\\monitor\\backend_overview.py\u001b[0m in \u001b[0;36mbackend_overview\u001b[1;34m()\u001b[0m\n\u001b[0;32m    156\u001b[0m     \u001b[0mbackends\u001b[0m \u001b[0mthat\u001b[0m \u001b[0mare\u001b[0m \u001b[0mavailable\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    157\u001b[0m     \"\"\"\n\u001b[1;32m--> 158\u001b[1;33m     \u001b[0munique_hardware_backends\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_unique_backends\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    159\u001b[0m     \u001b[0m_backends\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    160\u001b[0m     \u001b[1;31m# Sort backends by operational or not\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\.conda\\envs\\name_of_my_env\\lib\\site-packages\\qiskit\\tools\\monitor\\backend_overview.py\u001b[0m in \u001b[0;36mget_unique_backends\u001b[1;34m()\u001b[0m\n\u001b[0;32m     48\u001b[0m             \u001b[0munique_names\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mback\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     49\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0munique_hardware_backends\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 50\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mQiskitError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'No backends available.'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     51\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0munique_hardware_backends\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     52\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mQiskitError\u001b[0m: 'No backends available.'"
     ]
    }
   ],
   "source": [
    "provider = IBMQ.load_account()\n",
    "backend_overview()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also access more details of a particular computer with the following command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "QiskitBackendNotFoundError",
     "evalue": "'No backend matches the criteria'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mQiskitBackendNotFoundError\u001b[0m                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-85c4671f9849>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mbackend_monitor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprovider\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"ibmq_ourense\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\.conda\\envs\\name_of_my_env\\lib\\site-packages\\qiskit\\providers\\baseprovider.py\u001b[0m in \u001b[0;36mget_backend\u001b[1;34m(self, name, **kwargs)\u001b[0m\n\u001b[0;32m     44\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mQiskitBackendNotFoundError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'More than one backend matches the criteria'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     45\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mbackends\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 46\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mQiskitBackendNotFoundError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'No backend matches the criteria'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     47\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     48\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mbackends\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mQiskitBackendNotFoundError\u001b[0m: 'No backend matches the criteria'"
     ]
    }
   ],
   "source": [
    "backend_monitor(provider.get_backend(\"ibmq_ourense\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are going to select the least busy device and run our circuit on it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from qiskit.providers.ibmq import least_busy\n",
    "\n",
    "# We execute on the least busy device (among the actual quantum computers)\n",
    "backend = least_busy(provider.backends(operational = True, simulator=False, status_msg='active',\n",
    "                                       filters=lambda x: x.configuration().n_qubits > 1)) \n",
    "print(\"We are executing on...\",backend)\n",
    "print(\"It has\",backend.status().pending_jobs,\"pending jobs\")\n",
    "job_exp = execute(circ_bell, backend=backend)\n",
    "job_monitor(job_exp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once that we have run the job, we draw the results and compare them to the one obtained by the simulator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_exp = job_exp.result()\n",
    "counts_exp = result_exp.get_counts(circ_bell)\n",
    "plot_histogram([counts_exp,counts], legend=['Device', 'Simulator'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
